package com.dongdongshop.Controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpeOptionService;
import com.dongdongshop.service.SpecificationService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("specificationController")
public class SpecificationController {

    @Reference(timeout = 5000)
    private SpecificationService specificationService;
    @Reference
    private SpeOptionService speOptionService;

    @RequestMapping("toSpecification")
    public String toSpecification(){
        return "admin/specification";
    }

    @RequestMapping("findSpecificationAll")
    @ResponseBody
    public Result findList(String specName){
        List<TbSpecification> list = specificationService.findAll(specName);
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }
    @RequestMapping("addInfo")
    @ResponseBody
    public Result addInfo(TbSpecification tbSpecification,String optionListJson){
        List<TbSpecificationOption> options = JSONObject.parseArray(optionListJson, TbSpecificationOption.class);
        specificationService.insertSpec(tbSpecification,options);
        return  Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public Result toUpdate(@PathVariable("id") Long id){
        Map<Object,Object> map = speOptionService.selectById(id);

        return  Result.build(ConstantEnum.SUCCESS).setResult(map);
    }

    @RequestMapping("deleteInfo/{id}")
    @ResponseBody
    public Result deleteInfo(@PathVariable("id") Long id){
        speOptionService.deleteById(id);
        return  Result.build(ConstantEnum.SUCCESS);
    }

}
